package com.ebelter.seneo.ui.activitys.sets;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.ui.activitys.SeneoBaseActivity;
import com.ebelter.seneo.ui.fragments.main.Dash_Fragment;
import com.ebelter.seneo.ui.fragments.main.Set_Fragment;
import com.ebelter.seneo.ui.fragments.main.Trend_Fragment;
import com.ebelter.seneo.utils.SeneoUtils;

public class SwitchUnitActivity extends SeneoBaseActivity implements View.OnClickListener {

    public static final String TAG = "SwitchUnitActivity";

    ImageView top_iv;
    TextView top_tv1;
    ImageView sw_kg_iv;
    ImageView sw_lb_iv;
    TextView save_bt;

    RelativeLayout sw_kg_rl;
    RelativeLayout sw_lb_rl;

    private int selectUnitModel;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_switch_unit;
    }

    @Override
    protected void initView() {
        FV();
        displayUI(SeneoUtils.getUnitModel());
        setlistener();
    }

    private void setlistener() {
        sw_kg_rl.setOnClickListener(this);
        sw_lb_rl.setOnClickListener(this);
        save_bt.setOnClickListener(this);
        top_iv.setOnClickListener(this);

    }


    private void FV() {
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        sw_kg_iv=findViewById(R.id.sw_kg_iv);
        sw_lb_iv=findViewById(R.id.sw_lb_iv);
        save_bt=findViewById(R.id.save_bt);
        sw_kg_rl=findViewById(R.id.sw_kg_rl);
        sw_lb_rl=findViewById(R.id.sw_lb_rl);
    }

    private void displayUI(int unit){
        selectUnitModel=unit;
        ViewUtils.hideView(sw_kg_iv);
        ViewUtils.hideView(sw_lb_iv);
        ViewUtils.displayView(unit==0?sw_kg_iv:sw_lb_iv);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_iv:
                finish();
             break;
            case R.id.save_bt:
                save();
                break;
            case R.id.sw_kg_rl:
                displayUI(0);
                break;
            case R.id.sw_lb_rl:
                displayUI(1);
                break;
        }
    }

    private void save() {


        int srcUnitModel = SeneoUtils.getUnitModel();
        ToastUtil.show(R.string.Modify_Success);
        if (srcUnitModel==selectUnitModel){
            finish();
        }else {
            UserSpUtil.writeInt(IUserContant.IUser.userUnitModel,selectUnitModel);
            SeneoUtils.sendCommonEventBus(TAG, Set_Fragment.TAG+ Dash_Fragment.TAG+ Trend_Fragment.TAG,IUserContant.ICMDS.UPDATE_UNIT_DISPLAY,"更新单位显示");
            SeneoUtils.updateCurrentInfoDiaplay(TAG);
            finish();
        }

    }


}
